import java.awt.Point;
import java.awt.Rectangle;
import java.util.*; 
import java.awt.Point;
import java.awt.Rectangle;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

public class Deck implements java.io.Serializable{

	private static final String DECK_PATH = "./decks/";
	private static final String DECK_EXTENSION = ".deck";
	
	private List<Card> cards;
	
	private HashMap<Integer,Card> idToCard = new HashMap<Integer,Card>();
	
	public Deck(int numCards) {
		
		cards = new ArrayList<Card>();
		
		for(int i = 0; i < numCards; i++) {
			cards.add(new Card(i));
		}
	}
	
	public Deck(String deck) {
		
		String line = "";
		String[] args;
		HashMap<String, String> backs = new HashMap<String, String>();
		Vector<Card> tmpCards = new Vector<Card>();
		String name, back, image;
		int cardId = 0;
		
		try {
			BufferedReader br = new BufferedReader(new FileReader(DECK_PATH + deck + "/" + deck + DECK_EXTENSION));
			
			//read a line at a time
			while((line = br.readLine()) != null) {
				args = line.split(",");
				
				//get card backs
				if(args[0].equalsIgnoreCase("back")) {
					backs.put(args[1], DECK_PATH + deck + "/" + args[2]);
				//get card data
				} else if (args[0].equalsIgnoreCase("card")) {
					name = args[1];
					back = args[2];
					image = DECK_PATH + deck + "/" + args[3];
					Card newCard = new Card(cardId, image, backs.get(back), name);
						
					//get attributes
					for (int i = 4; i < args.length; i += 2) {
						newCard.addAttribute(args[i], args[i+1]);
					}
					
					tmpCards.add(newCard);
					cardId++;
				}
			}
			
			cards = new ArrayList<Card>(tmpCards);
			for (Card c: cards) {
				idToCard.put(c.getID(), c);
			}
			
			CardsServer.setSnapX(cards.get(0).getRectangle().width);
			CardsServer.setSnapY(cards.get(0).getRectangle().height);
			
		} catch (IOException e) {
			System.err.println("IO problems");
			e.printStackTrace();
		}
	}
	
	public Card getCard(int index) throws IndexOutOfBoundsException {
		return cards.get(index);
	}
	
	public Card getCardFromID(int id) {
		return idToCard.get(id);
	}
	
	public List<Card> getDeck() {
		return cards;
	}
	
	
	///////////////////////////////////////////////////////////
	//////////////METHODS EXTENDED FROM CARD.JAVA//////////////
	///////////////////////////////////////////////////////////
	
/*	public void setID(int index, int ID) {
		try {
			getCard(index).setID(ID);
		} catch (IndexOutOfBoundsException e) {
			
		}
	}
	
	public int getID(int index) {
		try {
			return getCard(index).getID(ID);
		} catch (IndexOutOfBoundsException e) {
			return -1;
		}
	}
	
	public int getImmutableID(int index) {
		if(index >= cards.size()) {
			return -1;
		}
		else {
			return cards[index].getImmutableID();
		}
	}
	
	public Rectangle getRectangle(int index) {
		if(index >= cards.size()) {
			return null;
		}
		else {
			return cards[index].getRectangle();
		}
	}
	
	public Point getLocation(int index) {
		if(index >= cards.length) {
			return null;
		}
		else {
			return cards[index].getLocation();
		}
	}

	public void setRectangle(int index, Rectangle cardRec) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setRectangle(cardRec);
		}
	}
	
	public void setSize(int index, int x, int y) {
		 if(index >= cards.length) {
			
		}
		else {
			cards[index].setSize(x, y);
		}
		
	}

	public void setLocation(int index, Point location) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setLocation(location);
		}
	}
	
	public void setLocation(int index, int x, int y) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setLocation(x, y);
		}
	}
	
	public int getZ(int index) {
		if(index >= cards.length) {
			return -1;
		}
		else {
			return cards[index].getZ();
		}
	}

	public void setZ(int index, int z) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setZ(z);
		}
	}
	
	public void setFaceUp(int index) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setFaceUp();
		}
	}
	
	public void setFaceDown(int index) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setFaceDown();
		}
	}
	
	public void toggleFaceUp(int index) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].toggleFaceUp();
		}
	}
	
	public boolean isFaceUp(int index) {		//default false
		if(index >= cards.length) {
			return false;
		}
		else {
			return cards[index].isFaceUp();
		}
	}
	
	public boolean isFlipable(int index) {		//default true
		if(index >= cards.length) {
			return true;
		}
		else {
			return cards[index].isFlipable();
		}
	}
	
	public void setFlipable(int index, boolean flip) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setFlipable(flip);
		}
	}
	
	public boolean isMoveable(int index) {		//default true
		if(index >= cards.length) {
			return true;
		}
		else {
			return cards[index].isMoveable();
		}
	}
	
	public void setMoveable(int index, boolean move) {
		if(index >= cards.length) {
			
		}
		else {
			cards[index].setMoveable(move);
		}
	}
	
	public Image getImage(int index){
		if(index >= cards.length) {
			return null;
		}
		else {
			return cards[index].getImage();
		}
	}*/
	
}
